<?php
namespace  App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\GarbageFee;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GarbageFeeController extends Controller
{
    /*
     * 垃圾费列表
     */
    public function index(Request $request,GarbageFee $garbageFee){
        $communityId = $request->input('community_id', 1);
        if(isset($request->condition) && !empty($request->condition)){
            $res = $garbageFee->indexPageLike($request->page,$request->limit,$request->conditio,$communityId);
        }else{
            $res = $garbageFee->indexPage($request->page,$request->limit,$communityId);
        }
        if($res && count($res) >0){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','数据为空');
        }
    }
    /*
     * 垃圾费记录数
     */
    public function indexNum(Request $request,GarbageFee $garbageFee)
    {
        $communityId = $request->input('community_id', 1);
        if(isset($request->condition) && !empty($request->condition)){
            $condition = $request->condition;
            $query = 'SELECT COUNT(*) as count FROM `garbage_fee` WHERE `status` = 1 and CONCAT(`house_number`,`username`) LIKE ?';
            $param = '%'.$condition.'%';
            $count = DB::select($query,array($param));
            $res = $count[0]->count;
        }else{
            $res = $garbageFee->where('status',1)->count();
        }
        if($res){
            return $this->return_json(200,$res,'查询成功');
        }else{
            return $this->return_json(201,'','数据为空');
        }
    }

    public function delete(Request $request,GarbageFee $garbageFee){
        $this->validate($request,[
            'id' => 'required'
        ],[
            'id.required' => 'id不能为空'
        ]);
        $param = $request->all();
        $param['status'] = 0;
        $code = $garbageFee->whereid($request->id)->update($param);
        if($code){
            return $this->return_json(200,'','删除成功');
        }else{
            return $this->return_json(201,'','删除失败');
        }
    }
}
